/*******************************************************************************
* Copyright (c) 2010, 2018 西安秦晔信息科技有限公司
* Licensed under the Apache License, Version 2.0 (the "License");
*    you may not use this file except in compliance with the License.
*    You may obtain a copy of the License at
*
*       http://www.apache.org/licenses/LICENSE-2.0
*
*    Unless required by applicable law or agreed to in writing, software
*    distributed under the License is distributed on an "AS IS" BASIS,
*    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*    See the License for the specific language governing permissions and
*    limitations under the License.
*******************************************************************************/
package com.qinyeit.serviceapp.service.members.impl;

import com.google.common.collect.Lists;
import com.qinyeit.serviceapp.entity.MemberCard;
import com.qinyeit.serviceapp.entity.VipRecord;
import com.qinyeit.serviceapp.entity.enums.Gender;
import com.qinyeit.serviceapp.entity.enums.State;
import com.qinyeit.serviceapp.exception.BusinessRunTimeException;
import com.qinyeit.serviceapp.service.members.MemberCardService;
import com.qinyeit.serviceapp.service.members.VipRecordService;
import com.qinyeit.serviceapp.utils.SearchUtils;
import com.qinyetech.springstage.core.entity.search.SearchOperator;
import com.qinyetech.springstage.core.entity.search.Searchable;
import com.qinyetech.springstage.core.lang.QyMap;
import com.qinyetech.springstage.core.lang.StringUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.qinyetech.springstage.core.service.BaseService;
import com.qinyeit.serviceapp.entity.Member;
import com.qinyeit.serviceapp.repository.members.MemberRepository;
import com.qinyeit.serviceapp.service.members.MemberService;

import java.util.List;
import java.util.stream.Collectors;

/** 
 * ClassName: MemberServiceImpl <br/> 
 * Function: 会员信息 业务实现 <br/> 
 * date: 2018-12-04 11:16:11 <br/> 
 * 
 * @author liuwenjin
 * @version 1.0
 * @since JDK 1.8
 */
@Service
@Transactional
public class MemberServiceImpl extends BaseService<Member, Long> implements MemberService {


	@Autowired
	private MemberRepository memberRepository;
	@Autowired
	private MemberCardService memberCardService;
	@Autowired
	private VipRecordService vipRecordService;

	/**
	 * 保存 或 编辑 会员
	 * @param vipData
	 */
	@Override
	public Member updateMember(QyMap vipData, Long merchantGroupId,String branchName) {
		Searchable searchable = SearchUtils.newSearchableWithMerchantFilter(merchantGroupId);
//		searchable.addSearchParam("phone_eq",vipData.get("phone"));
		searchable.addSearchParam("uuid_eq",vipData.get("uuid"));
		Member member = this.findOne(searchable);
		if (member==null){
			//添加会员
			member = new Member();
			member.setMerchantGroupId(merchantGroupId);
			member.setBranchName(branchName);
			member.setUuid(vipData.getString("uuid"));
			log.info("入会门店 ： {}",branchName);
		}
		member.setPhone(vipData.getString("phone"));
		member.setBirthday(vipData.getTime("birthday"));
		member.setBirthdayType(vipData.getString("birthday_type"));
		member.setCardNo(vipData.getString("cardNo"));
		member.setConsumeAmount(vipData.getDouble("consume_amount"));
		member.setConsumeCount(vipData.getInt("consume_count"));
		member.setConsumeTime(vipData.getTime("consume_time"));
		member.setGender(Gender.valueOf(vipData.getString("gender")));
		member.setName(vipData.getString("name"));
		member.setSource(vipData.getString("source"));
		member.setState(State.valueOf(vipData.getString("state")));
		return this.update(member);
	}

	@Override
	public Long getCardCount(Long memberId,Long merchantGroupId) {
		Searchable searchable = SearchUtils.newSearchableWithMerchantFilter(merchantGroupId);
		searchable.addSearchParam("vipId_eq",memberId);
		return memberCardService.count(searchable);
	}

	@Override
	public List<MemberCard> getMemberCard(Long id,Long merchantGroupId) {
		Searchable searchable = SearchUtils.newSearchableWithMerchantFilter(merchantGroupId);
		searchable.addSearchParam("vipId_eq",id);
		return memberCardService.findWithSort(searchable);
	}

	@Override
	public Page<VipRecord> getVipRecord(Long id, Long merchantGroupId,Searchable searchable) {
		Member member = this.findById(id);
		searchable = SearchUtils.newSearchableWithMerchantFilter(searchable,merchantGroupId);
		searchable.addSearchParam("vipPhone_eq",member.getPhone());
		return vipRecordService.findAll(searchable);
	}

	@Override
	public List<Long> getIdsByCardNo(String no,Long merchantGroupId) {
		List<Long> ids = Lists.newArrayList();
		if(StringUtils.isBlank(no)){
			return ids;
		}
		Searchable searchable = SearchUtils.newSearchableWithMerchantFilter(merchantGroupId);
		searchable.addSearchFilter("vipId", SearchOperator.isNotNull,null);
		searchable.addSearchParam("no_like",no);
		List<MemberCard> memberCardList = memberCardService.findWithSort(searchable);
		if(CollectionUtils.isEmpty(memberCardList)){
			ids.add(-1L);
			return ids;
		}
		ids = memberCardList.stream().map(x->x.getVipId()).collect(Collectors.toList());
		return ids;
	}

}